//package com.pubinfo.passbook.common.aspect;
//
//import com.alibaba.fastjson.JSONObject;
//import com.pubinfo.passbook.common.annotation.RequiredLog;
//import com.pubinfo.passbook.common.utils.IPUtils;
//import com.pubinfo.passbook.common.utils.SessionCookieUtils;
//import com.pubinfo.passbook.common.entity.TSystemOperationLog;
//import com.pubinfo.passbook.common.entity.TSystemUser;
//import com.pubinfo.passbook.common.mapper.TSystemOperationLogMapper;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authz.UnauthorizedException;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.Signature;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import javax.servlet.http.HttpServletRequest;
//import java.io.UnsupportedEncodingException;
//import java.lang.reflect.Method;
//import java.net.URLDecoder;
//
//@Aspect
//@Service
//public class SysLogAspect {//日志切面
//  @Autowired
//  private TSystemOperationLogMapper operationLogMapper;
//
//  //@annotation()为细粒度的切入点表达式定义方式
//  // @Around("@annotation(com.pubinfo.passbook.annotation.RequiredLog)")
//  @Around("execution(* com.pubinfo.passbook.controller.auth.*..*(..))")
//  public Object aroundMethod(ProceedingJoinPoint jp)
//      throws Throwable {
//    //1.目标业务执行之前的记录
//    long t1 = System.currentTimeMillis();
//    //2.执行目标业务(底层通过反射执行目标方法)
//    Object result = jp.proceed();
//    //3.目标业务执行之后的记录
//    long t2 = System.currentTimeMillis();
//    // saveObject(jp, (t2 - t1));  暂时不记录
//    //4.返回目标业务的执行结果
//    return result;
//  }
//
//  private void saveObject(ProceedingJoinPoint jp, long time) throws Exception {
//    //1.获取要保存的日志信息
//    //1.1获取登陆用户
//    TSystemUser user = (TSystemUser) SecurityUtils.getSubject().getPrincipal();
//    if (user == null) {
//      throw new UnauthorizedException("请重新登录");
//    }
//    //1.2获取方法签名
//    Signature s = jp.getSignature();
//    MethodSignature ms = (MethodSignature) s;
//    //1.2.1获取目标对象
//    Class<?> targetClass = jp.getTarget().getClass();
//    //1.2.2基于目标业务对象获取要执行的目标方法
//    Method targetMethod = targetClass.getDeclaredMethod(
//        ms.getName(),
//        ms.getParameterTypes());
//    //1.2.3获取方法上定义的注解内容(定义的操作名)
//    RequiredLog requiredLog =
//        targetMethod.getDeclaredAnnotation(RequiredLog.class);
//    String operation = null;
//    if (requiredLog != null) {
//      operation = requiredLog.value();
//    }
//    //1.2.4获取目标对象方法的全称(类全名+方法名)
//    String targetClassName = targetClass.getName();
//    String targetMethodName = targetClassName + "." + targetMethod.getName();
//    //1.3获取方法执行时的实际参数
//    // JSON.parseObject(jp.getArgs());
//    String params = JSONObject.toJSONString(jp.getArgs());
//    //	String params=Arrays.toString(jp.getArgs());
//    //2.封装日志信息
//    TSystemOperationLog log = new TSystemOperationLog();
//    log.setUsername(user.getUsername());
//    log.setAction(getHeaderByName("x-action"));
//    log.setMenu(getHeaderByName("menu"));
//    log.setSubMenu(getHeaderByName("subMenu"));
//    log.setIp(IPUtils.getIpAddr());
//    log.setOperation(operation);
//    log.setMethod(targetMethodName);
//    log.setParams(params);
//    log.setTime(time);
//    //3.将日志信息写入到数据库
//    operationLogMapper.insert(log);
//  }
//
//  public String getHeaderByName(String name) {
//    String item = null;
//    HttpServletRequest request = SessionCookieUtils.getHttpServletRequest();
//    if (request != null) {
//      return decode(request.getHeader(name));
//    }
//    return item;
//  }
//
//  public String decode(String url) {
//
//    try {
//      if (StringUtils.isBlank(url)) {
//        return null;
//      }
//      String prevURL = "";
//
//      String decodeURL = url;
//
//      while (!prevURL.equals(decodeURL)) {
//        prevURL = decodeURL;
//        decodeURL = URLDecoder.decode(decodeURL, "UTF-8");
//      }
//
//      return decodeURL;
//
//    } catch (UnsupportedEncodingException e) {
//
//      return "Issue while decoding" + e.getMessage();
//
//    }
//
//  }
//}
//
//
//
//
//
//
//
